using System;
using System.Web.UI;
using System.Web.UI.Design;
using UNLV.IAP.WebControls;


namespace UNLV.IAP.WebControls
{
	/// <summary>
	/// Provides design-time capabilities for the SqlWhereBuilder web control.
	/// </summary>
	public class SqlWhereBuilderDesigner : ControlDesigner
	{
		public override string GetDesignTimeHtml()
		{
			string sHtml  = null;
			try
			{
				sHtml = RenderSampleHtml();
			}
			catch (Exception ex)
			{
				sHtml = this.GetErrorDesignTimeHtml(ex);
			}
			
			if ((sHtml == null) || (sHtml.Length == 0))
			{
				sHtml = this.GetEmptyDesignTimeHtml();
			}

			return sHtml;
		}


		private string RenderSampleHtml()
		{
			SqlWhereBuilder wb = (SqlWhereBuilder) this.Component;

			// create a mock-up of what the builder may look like
			// given the current set of style properties

			string sClass = (wb.MainClass == "" ? "" : " class=\"" + wb.MainClass + "\"");
			string sStyle = (wb.MainStyle == "" ? "" : " style=\"" + wb.MainStyle + "\"");
			string sPadding = (wb.Padding == "" ? "" : " cellpadding=\"" + wb.Padding + "\"");
			string sSpacing = (wb.Spacing == "" ? "" : " cellspacing=\"" + wb.Spacing + "\"");
			string sBorder  = (wb.Border  == "" ? "" : " border=\"" + wb.Border + "\"");


			// sample conditions display
			string sData = "";
			string sButtons = "";
			string sConditions = "<table width='100%' style='border-collapse: collapse;' cellpadding='0' cellspacing='0' >";

			string sEditButtonsClass = (wb.EditButtonsClass == "" ? "" : " class=\"" + wb.EditButtonsClass + "\"");
			string sEditButtonsStyle = (wb.EditButtonsStyle == "" ? "" : " style=\"" + wb.EditButtonsStyle + "\"");
			string sConditionDisplayClass = (wb.ConditionDisplayClass == "" ? "" : " class=\"" + wb.ConditionDisplayClass + "\"");
			string sConditionDisplayStyle = (wb.ConditionDisplayStyle == "" ? "" : " style=\"" + wb.ConditionDisplayStyle + "\"");
			string sConditionCellClass = (wb.ConditionCellClass == "" ? "" : " class=\"" + wb.ConditionCellClass + "\"");
			string sConditionCellStyle = (wb.ConditionCellStyle == "" ? "" : " style=\"" + wb.ConditionCellStyle + "\"");
			string sNoConditionsClass = (wb.NoConditionsClass == "" ? "" : " class=\"" + wb.NoConditionsClass + "\"");
			string sNoConditionsStyle = (wb.NoConditionsStyle == "" ? "" : " style=\"" + wb.NoConditionsStyle + "\"");
    
			// condition 1
			sData = "<td width=\"100%\"" + sConditionDisplayClass + sConditionDisplayStyle + "> Sample First Condition</td>";
			sButtons = "<td " + sEditButtonsClass + sEditButtonsStyle + " bgcolor=\"" + wb.EditButtonsColor + "\" align='center' onmouseover='this.style.backgroundColor=\"" + wb.EditButtonsHiliteColor + "\";' onmouseout='this.style.backgroundColor=\"" + wb.EditButtonsColor + "\";'>" + wb.DeleteButtonText + "</td>"
				+ "<td " + sEditButtonsClass + sEditButtonsStyle + " bgcolor=\"" + wb.EditButtonsColor + "\" align='center' onmouseover='this.style.backgroundColor=\"" + wb.EditButtonsHiliteColor + "\";' onmouseout='this.style.backgroundColor=\"" + wb.EditButtonsColor + "\";'>" + wb.EditButtonText + "</td>";

			string sRow = "<table cellpadding='2' cellspacing='0'>"
				+ " <tr>"
				+ sButtons
				+ "   <td>&nbsp</td>"
				+ sData
				+ "   <td>&nbsp</td>"
				+ " </tr>"
				+ "</table>";

			sConditions += "<tr><td" + sConditionCellClass + sConditionCellStyle + ">" + sRow + "</td></tr>";

			// condition 2
			sData = "<td width=\"100%\"" + sConditionDisplayClass + sConditionDisplayStyle + "> and Sample Second Condition</td>";

			sRow = "<table cellpadding='2' cellspacing='0'>"
				+ " <tr>"
				+ sButtons
				+ "   <td>&nbsp</td>"
				+ sData
				+ "   <td>&nbsp</td>"
				+ " </tr>"
				+ "</table>";

			sConditions += "<tr><td" + sConditionCellClass + sConditionCellStyle + ">" + sRow + "</td></tr>";
            sConditions += "</table>";


			// entry form sample
			string divId = wb.ClientID;
			string sOptionsFields = "<option>Field 1</option><option>Field 2</option><option>Field 3</option>";
			string sOptionsOps = "<option>=</option><option>!=</option><option>&gt;</option><option>&lt;</option>";
			string sButtonClass = (wb.ButtonClass == "" ? "" : " class=\"" + wb.ButtonClass + "\"");
			string sButtonStyle = (wb.ButtonStyle == "" ? "" : " style=\"" + wb.ButtonStyle + "\"");

			sButtons = "<input type=\"button\" " 
				+ sButtonClass + sButtonStyle + " value=\"" + wb.AddButtonText + "\">";
			
			
			string sForm = "<table style=\"border-collaspse: collapse; border: 1px solid black;\""
				+ " width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">"
				+ " <tr valign=\"top\"><td width=\"100%\" bgcolor=\"" + wb.FormColor + "\">"
				+ " <div id=\"" + divId + "_andOrDiv\" style=\"display:inline;\"><select id=\"" + divId + "_andOr\"><option value=\"and\">and</option><option value=\"or\">or</option></select>&nbsp;</div>"
				+ " <select id=\"" + divId + "_fields\"     >" + sOptionsFields + "</select>"
				+ " <select id=\"" + divId + "_operators\" >" + sOptionsOps + "</select>"
				+ " <div id=\"" + divId + "_valueEntry\" style=\"display:inline;\"><input type='text' value='Sample' /></div>"
				+ " <div id=\"" + divId + "_entryButtons\" style=\"display:inline;\">&nbsp;" + sButtons + "</div>"
				+ "</td></tr></table>";




			// put it all together
			string sTable = "<table" + sClass + sStyle + sPadding + sSpacing + sBorder + ">"
				+ "  <tr valign='top'>"
				+ "    <td>"
				+ "      <div id=\"" + divId + "_conditions\">"
				+ sConditions
				+ "      </div>"
				+ "    </td>"
				+ "  </tr>"
				+ "  <tr valign='top'>"
				+ "    <td>"
				+ "      <div id=\"" + divId + "_form\">"
				+ sForm
				+ "      </div>"
				+ "    </td>"
				+ "  </tr>"
				+ "</table>";

			return sTable;
			
		}

	}

	
}
